import { logout } from "@/store/actions/login";
import { isAuth } from "@/utils/token";
import { useDispatch } from "react-redux";
import { Route, Redirect, RouteProps, useLocation } from "react-router-dom";
export const AuthRoute = ({ children, ...rest }: RouteProps) => {
  const location = useLocation();
  const dispatch: any = useDispatch();
  return (
    <Route
      {...rest}
      render={(): any => {
        const isLogin = isAuth();
        if (isLogin) {
          // 登录
          return children;
          // return <Home />
        }

        dispatch(logout());
        // 未登录
        return (
          <Redirect
            to={{
              pathname: "/login",
              state: {
                from: location.pathname,
              },
            }}
          />
        );
      }}
    />
  );
};
